package com.hengpeng.itfinbase.idAssign;

import java.io.Serializable;
import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.type.Type;

/**
 * 主键生成器
 * 
 * @author weiyuan
 */
public class IdGenerator implements IdentifierGenerator, Configurable {
    /**
     * 主键长度
     */
    public String idLength;

    @Override
    public void configure(Type type, Properties params, Dialect d) throws MappingException {
        this.idLength = params.getProperty("idLength");
    }

    @Override
    public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
        int length = Integer.valueOf(idLength);
        return ID.getInstanse().getID(length);
    }

    public static void main(String[] args) {
        System.out.println(ID.getInstanse().getID(19));
    }
}